﻿// Copyright 2006 - Ricardo Stuven (rstuven@gmail.com)
//
// This file is part of MsSqlSpatial.
// MsSqlSpatial is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// MsSqlSpatial is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.

// You should have received a copy of the GNU Lesser General Public License
// along with MsSqlSpatial; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using GeoAPI.Geometries;
using Microsoft.SqlServer.Server;
using MsSqlSpatial;

public partial class TableValuedFunctions
{
    [SpatialQuery]
    [SqlFunction(
        FillRowMethodName = SpatialQuery.FillRow
        , TableDefinition = SpatialQuery.TableDefinition
        , DataAccess = DataAccessKind.Read
        , SystemDataAccess = SystemDataAccessKind.Read
        )]
    public static IEnumerable EqualsExactQuery(
        SqlString table_name,
        SqlString column_name,
        SqlBytes ewkb,
        SqlDouble tolerance)
    {
        return SpatialQuery.Execute(table_name, column_name, ewkb, new SpatialQuery.Arguments(tolerance),
                                    EqualsExactQueryHandler);
    }

    [SpatialQuery]
    [SqlFunction(
        FillRowMethodName = SpatialQuery.FillRow
        , TableDefinition = SpatialQuery.TableDefinition
        , DataAccess = DataAccessKind.Read
        , SystemDataAccess = SystemDataAccessKind.Read
        )]
    public static IEnumerable EqualsExactQueryWhere(
        SqlString table_name,
        SqlString column_name,
        SqlBytes ewkb,
        SqlDouble tolerance,
        SqlString condition)
    {
        return SpatialQuery.Execute(table_name, column_name, ewkb, condition, new SpatialQuery.Arguments(tolerance),
                                    EqualsExactQueryHandler);
    }

    private static IEnumerable EqualsExactQueryHandler(SqlDataReader reader, IGeometry geometry,
                                                       SpatialQuery.Arguments args)
    {
        double tolerance;
        if (args.double1.IsNull)
            tolerance = 0;
        else
            tolerance = args.double1.Value;

        var oids = new List<Int64>();
        while (reader.Read())
        {
            IGeometry _geometry = ConvertGeometry.ToGeometry(reader.GetSqlBytes(1));
            if (geometry.EqualsExact(_geometry, tolerance))
            {
                oids.Add((long) reader.GetValue(0));
            }
        }
        return oids;
    }
} ;